package base;


import android.app.Application;

import com.xiangxue.network.base.INetworkRequiredInfo;
import com.xiangxue.network.base.NetworkApi;
import com.xiangxue.network.errorhandler.HttpErrorHandler;
import com.xiangxue.network.observer.BaseObserver;

import java.util.HashMap;

import acitivity.MyApplication;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import mInterface.ApiUrl;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Url;
import util.UtilX;

public class NetBase extends NetworkApi{

    private static volatile NetBase share=null;

    public  static String URL_BASE = "http://www.ygzctech.com:8781/api/";
    //登录
    public  String   url_login = URL_BASE + "user/login";

    private Retrofit loginRetrofit;

    public  void initURL(){
        url_login = URL_BASE + "user/login";
    }


    public static NetBase getInstance() {
        if(share==null){
            share=new NetBase();
        }
        return share;
    }

    public NetBase(){
        initURL();
    }



    public void baseInit(String versionname,String versioncode,boolean isDebug,Application application){
        NetworkApi.init(new INetworkRequiredInfo() {
            @Override
            public String getAppVersionName() {
                return versionname;
            }

            @Override
            public String getAppVersionCode() {
                return versioncode;
            }

            @Override
            public boolean isDebug() {
                return isDebug;
            }

            @Override
            public Application getApplicationContext() {
                return application;
            }
        });
    }


    
    public void Login(String username,String passwd,BaseObserver<okhttp3.ResponseBody> observer){
        FormBody body = new FormBody.Builder()
                .add("phone", username)
                .add("password", passwd)
                .add("device", MyApplication.uniqueId)
                .add("devicetype", "android")
                .build();
        loginRetrofit=getRetrofit(ApiUrl.PostData.class);
        ApiUrl.PostData apiUrl= loginRetrofit.create(ApiUrl.PostData.class);
        Observable<okhttp3.ResponseBody> observable=apiUrl.applicationPostFormBodyJsonObject(url_login,body);
        observable.subscribeOn(Schedulers.io()).subscribe(observer);
    }


    @Override
    protected Interceptor getInterceptor() {
        return null;
    }

    @Override
    protected <T> Function<T, T> getAppErrorHandler() {
        return (Function<T, T>) new HttpErrorHandler<Response<String>>();
    }

    //正式根地址
    @Override
    public String getFormal() {
        return URL_BASE;
    }

    @Override
    public String getTest() {
        return URL_BASE;
    }
}
